package com.xjh.t.common;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xjh.t.service.IGroupAttrInfoService;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

public class DynamicDbTool {

   public static String getOrderDBById(Integer id)
   {
       if(id == 1)
           return "orderDB";
       else
           return "orderDB";
   }
   //所有订单分库的ID
   public static final Integer[] ORDEDBIDS = {1,2};

    public static void switchDb(Class service,Integer dbId) throws NoSuchFieldException, IllegalAccessException {

        System.out.println("这是severc" + service.getClass().getSimpleName());
        DS annoTable = (DS) service.getAnnotation(DS.class);
        if (annoTable == null) {
            throw new RuntimeException("please add @DS for Test");
        }
// 获取代理处理器
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(annoTable);
// 过去私有 memberValues 属性
        Field f = invocationHandler.getClass().getDeclaredField("memberValues");
        f.setAccessible(true);
// 获取实例的属性map
        Map<String, Object> memberValues = (Map<String, Object>) f.get(invocationHandler);
// 修改属性值
        memberValues.put("value", getOrderDBById(dbId));
    }
}
